package com.app.piemoment.api;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;

import com.app.piemoment.config.Config;
import com.app.piemoment.entity.LoginInfo;
import com.app.piemoment.net.HttpResult;
import com.app.piemoment.net.HttpResultJson;
import com.app.piemoment.net.NetService;

public class QAAPI extends BaseApi {

	public QAAPI(Context context) {
		super(context);
	}

	/**
	 * 获取随机的一个问题
	 * 
	 * @param requestCode
	 * @param context
	 */
	public void getRandomQuestion(final int requestCode, final Context context) {

		new AsyncTask<Object, Integer, ApiResult<Object>>() {
			@Override
			protected ApiResult<Object> doInBackground(Object... strs) {
				ApiResult<Object> apiResult = new ApiResult<Object>();
				apiResult.setResultCode(ApiResult.RESULT_FAIL);
				List<NameValuePair> params = new ArrayList<NameValuePair>();

				try {
					params.add(new BasicNameValuePair("token", URLEncoder
							.encode(LoginInfo.getInstance().getAccess_token(),
									"UTF-8")));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}

				HttpResultJson result = NetService.httpGetReturnJson(context,
						Config.HOME_URL + Config.RANDOM_QUESTION_URL, params);

				if (result.getResultCode() == HttpResult.RESULT_OK) {
					apiResult.setResultCode(ApiResult.RESULT_OK);
					JSONObject jsonObject = result.getJson();
					apiResult.setJsonEntity(jsonObject);
				} else {
					apiResult.setFailCode(result.getFailCode());
					apiResult.setFailMessage(result.getFailMessage());
				}
				return apiResult;
			}

			@Override
			protected void onPostExecute(ApiResult<Object> apiResult) {
				if (returnResultListener == null) {
					return;
				}
				if (apiResult.getResultCode() == ApiResult.RESULT_OK) {
					returnResultListener.onReturnSucceedResult(requestCode,
							apiResult);
				} else {
					returnResultListener.onReturnFailResult(requestCode,
							apiResult);
				}
			}

			@Override
			protected void onPreExecute() {
				if (returnResultListener == null) {
					return;
				}
				returnResultListener.onReturnLoading(requestCode);
				super.onPreExecute();
			}

		}.execute();
	}

	/**
	 * 获取个人的问题
	 * 
	 * @param requestCode
	 * @param context
	 */
	public void getPersonalQuestion(final int requestCode, final Context context) {

		new AsyncTask<Object, Integer, ApiResult<Object>>() {
			@Override
			protected ApiResult<Object> doInBackground(Object... strs) {
				ApiResult<Object> apiResult = new ApiResult<Object>();
				apiResult.setResultCode(ApiResult.RESULT_FAIL);
				List<NameValuePair> params = new ArrayList<NameValuePair>();

				try {
					params.add(new BasicNameValuePair("token", URLEncoder
							.encode(LoginInfo.getInstance().getAccess_token(),
									"UTF-8")));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}

				HttpResultJson result = NetService.httpGetReturnJson(context,
						Config.HOME_URL + Config.GET_PERSONAL_QUESTION_URL,
						params);

				if (result.getResultCode() == HttpResult.RESULT_OK) {
					apiResult.setResultCode(ApiResult.RESULT_OK);
					JSONObject jsonObject = result.getJson();
					apiResult.setJsonEntity(jsonObject);
				} else {
					apiResult.setFailCode(result.getFailCode());
					apiResult.setFailMessage(result.getFailMessage());
				}
				return apiResult;
			}

			@Override
			protected void onPostExecute(ApiResult<Object> apiResult) {
				if (returnResultListener == null) {
					return;
				}
				if (apiResult.getResultCode() == ApiResult.RESULT_OK) {
					returnResultListener.onReturnSucceedResult(requestCode,
							apiResult);
				} else {
					returnResultListener.onReturnFailResult(requestCode,
							apiResult);
				}
			}

			@Override
			protected void onPreExecute() {
				if (returnResultListener == null) {
					return;
				}
				returnResultListener.onReturnLoading(requestCode);
				super.onPreExecute();
			}

		}.execute();
	}

	/**
	 * 添加个人的问题
	 * 
	 * @param requestCode
	 * @param context
	 */
	public void addAnswer(final int requestCode, final Context context,
			final String code, final String answer) {

		new AsyncTask<Object, Integer, ApiResult<Object>>() {
			@Override
			protected ApiResult<Object> doInBackground(Object... strs) {
				ApiResult<Object> apiResult = new ApiResult<Object>();
				apiResult.setResultCode(ApiResult.RESULT_FAIL);
				List<NameValuePair> params = new ArrayList<NameValuePair>();

				params.add(new BasicNameValuePair("answer", answer));
				params.add(new BasicNameValuePair("token", LoginInfo
						.getInstance().getAccess_token()));

				HttpResultJson result = NetService.httpPostReturnJson(context,
						Config.HOME_URL + String.format(Config.ADD_QUESTION_URL,code), params);

				if (result.getResultCode() == HttpResult.RESULT_OK) {
					apiResult.setResultCode(ApiResult.RESULT_OK);
					JSONObject jsonObject = result.getJson();
					apiResult.setJsonEntity(jsonObject);
				} else {
					apiResult.setFailCode(result.getFailCode());
					apiResult.setFailMessage(result.getFailMessage());
				}
				return apiResult;
			}

			@Override
			protected void onPostExecute(ApiResult<Object> apiResult) {
				if (returnResultListener == null) {
					return;
				}
				if (apiResult.getResultCode() == ApiResult.RESULT_OK) {
					returnResultListener.onReturnSucceedResult(requestCode,
							apiResult);
				} else {
					returnResultListener.onReturnFailResult(requestCode,
							apiResult);
				}
			}

			@Override
			protected void onPreExecute() {
				if (returnResultListener == null) {
					return;
				}
				returnResultListener.onReturnLoading(requestCode);
				super.onPreExecute();
			}

		}.execute();
	}

	/**
	 * 修改个人的问题
	 * 
	 * @param requestCode
	 * @param context
	 */
	public void setAnswer(final int requestCode, final Context context,
			final String question, final String answer, final String code) {

		new AsyncTask<Object, Integer, ApiResult<Object>>() {
			@Override
			protected ApiResult<Object> doInBackground(Object... strs) {
				ApiResult<Object> apiResult = new ApiResult<Object>();
				apiResult.setResultCode(ApiResult.RESULT_FAIL);
				List<NameValuePair> params = new ArrayList<NameValuePair>();

				try {
					params.add(new BasicNameValuePair("question", question));
					params.add(new BasicNameValuePair("answer", answer));
					params.add(new BasicNameValuePair("token", URLEncoder
							.encode(LoginInfo.getInstance().getAccess_token(),
									"UTF-8")));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}

				HttpResultJson result = NetService.httpPutReturnJson(
						context,
						Config.HOME_URL
								+ String.format(Config.SET_QUESTION_URL, code),
						params);

				if (result.getResultCode() == HttpResult.RESULT_OK) {
					apiResult.setResultCode(ApiResult.RESULT_OK);
					JSONObject jsonObject = result.getJson();
					apiResult.setJsonEntity(jsonObject);
				} else {
					apiResult.setFailCode(result.getFailCode());
					apiResult.setFailMessage(result.getFailMessage());
				}
				return apiResult;
			}

			@Override
			protected void onPostExecute(ApiResult<Object> apiResult) {
				if (returnResultListener == null) {
					return;
				}
				if (apiResult.getResultCode() == ApiResult.RESULT_OK) {
					returnResultListener.onReturnSucceedResult(requestCode,
							apiResult);
				} else {
					returnResultListener.onReturnFailResult(requestCode,
							apiResult);
				}
			}

			@Override
			protected void onPreExecute() {
				if (returnResultListener == null) {
					return;
				}
				returnResultListener.onReturnLoading(requestCode);
				super.onPreExecute();
			}

		}.execute();
	}

	/**
	 * 删除个人的问题
	 * 
	 * @param requestCode
	 * @param context
	 */
	public void delAnswer(final int requestCode, final Context context,
			final String code) {

		new AsyncTask<Object, Integer, ApiResult<Object>>() {
			@Override
			protected ApiResult<Object> doInBackground(Object... strs) {
				ApiResult<Object> apiResult = new ApiResult<Object>();
				apiResult.setResultCode(ApiResult.RESULT_FAIL);
				List<NameValuePair> params = new ArrayList<NameValuePair>();

				params.add(new BasicNameValuePair("_method", "delete"));
				params.add(new BasicNameValuePair("token", LoginInfo
						.getInstance().getAccess_token()));

				HttpResultJson result = NetService.httpDelReturnJson(
						context,
						Config.HOME_URL
								+ String.format(Config.DEL_QUESTION_URL, code),
						params);

				if (result.getResultCode() == HttpResult.RESULT_OK) {
					apiResult.setResultCode(ApiResult.RESULT_OK);
					JSONObject jsonObject = result.getJson();
					apiResult.setJsonEntity(jsonObject);
				} else {
					apiResult.setFailCode(result.getFailCode());
					apiResult.setFailMessage(result.getFailMessage());
				}
				return apiResult;
			}

			@Override
			protected void onPostExecute(ApiResult<Object> apiResult) {
				if (returnResultListener == null) {
					return;
				}
				if (apiResult.getResultCode() == ApiResult.RESULT_OK) {
					returnResultListener.onReturnSucceedResult(requestCode,
							apiResult);
				} else {
					returnResultListener.onReturnFailResult(requestCode,
							apiResult);
				}
			}

			@Override
			protected void onPreExecute() {
				if (returnResultListener == null) {
					return;
				}
				returnResultListener.onReturnLoading(requestCode);
				super.onPreExecute();
			}

		}.execute();
	}

	/**
	 * 删除个人所有回答
	 * 
	 * @param requestCode
	 * @param context
	 */
	public void delAllAnswer(final int requestCode, final Context context) {

		new AsyncTask<Object, Integer, ApiResult<Object>>() {
			@Override
			protected ApiResult<Object> doInBackground(Object... strs) {
				ApiResult<Object> apiResult = new ApiResult<Object>();
				apiResult.setResultCode(ApiResult.RESULT_FAIL);
				List<NameValuePair> params = new ArrayList<NameValuePair>();

				params.add(new BasicNameValuePair("_method", "delete"));
				params.add(new BasicNameValuePair("token", LoginInfo
						.getInstance().getAccess_token()));

				HttpResultJson result = NetService.httpDelReturnJson(context,
						Config.HOME_URL + Config.DEL_ALL_QUESTION_URL, params);

				if (result.getResultCode() == HttpResult.RESULT_OK) {
					apiResult.setResultCode(ApiResult.RESULT_OK);
					JSONObject jsonObject = result.getJson();
					apiResult.setJsonEntity(jsonObject);
				} else {
					apiResult.setFailCode(result.getFailCode());
					apiResult.setFailMessage(result.getFailMessage());
				}
				return apiResult;
			}

			@Override
			protected void onPostExecute(ApiResult<Object> apiResult) {
				if (returnResultListener == null) {
					return;
				}
				if (apiResult.getResultCode() == ApiResult.RESULT_OK) {
					returnResultListener.onReturnSucceedResult(requestCode,
							apiResult);
				} else {
					returnResultListener.onReturnFailResult(requestCode,
							apiResult);
				}
			}

			@Override
			protected void onPreExecute() {
				if (returnResultListener == null) {
					return;
				}
				returnResultListener.onReturnLoading(requestCode);
				super.onPreExecute();
			}

		}.execute();
	}

	/**
	 * 获取对方的问题
	 * 
	 * @param requestCode
	 * @param context
	 */
	public void getPersonalQuestion(final int requestCode,
			final Context context, final String mid) {

		new AsyncTask<Object, Integer, ApiResult<Object>>() {
			@Override
			protected ApiResult<Object> doInBackground(Object... strs) {
				ApiResult<Object> apiResult = new ApiResult<Object>();
				apiResult.setResultCode(ApiResult.RESULT_FAIL);
				List<NameValuePair> params = new ArrayList<NameValuePair>();

				try {
					params.add(new BasicNameValuePair("token", URLEncoder.encode(LoginInfo.getInstance().getAccess_token(),"UTF-8")));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}

				HttpResultJson result = NetService.httpGetReturnJson(context,Config.HOME_URL+ String.format(Config.GET_OPPOSITE_QUESTION_URL, mid),params);

				if (result.getResultCode() == HttpResult.RESULT_OK) {
					apiResult.setResultCode(ApiResult.RESULT_OK);
					JSONObject jsonObject = result.getJson();
					apiResult.setJsonEntity(jsonObject);
				} else {
					apiResult.setFailCode(result.getFailCode());
					apiResult.setFailMessage(result.getFailMessage());
				}
				return apiResult;
			}

			@Override
			protected void onPostExecute(ApiResult<Object> apiResult) {
				if (returnResultListener == null) {
					return;
				}
				if (apiResult.getResultCode() == ApiResult.RESULT_OK) {
					returnResultListener.onReturnSucceedResult(requestCode,
							apiResult);
				} else {
					returnResultListener.onReturnFailResult(requestCode,
							apiResult);
				}
			}

			@Override
			protected void onPreExecute() {
				if (returnResultListener == null) {
					return;
				}
				returnResultListener.onReturnLoading(requestCode);
				super.onPreExecute();
			}

		}.execute();
	}

	/**
	 * 修改个人的问题
	 * 
	 * @param requestCode
	 * @param context
	 */
	public void setPersonInfo(final int requestCode, final Context context,
			final String question, final String answer, final String code) {

		new AsyncTask<Object, Integer, ApiResult<Object>>() {
			@Override
			protected ApiResult<Object> doInBackground(Object... strs) {
				ApiResult<Object> apiResult = new ApiResult<Object>();
				apiResult.setResultCode(ApiResult.RESULT_FAIL);
				List<NameValuePair> params = new ArrayList<NameValuePair>();

				try {
					params.add(new BasicNameValuePair("question", question));
					params.add(new BasicNameValuePair("answer", answer));
					params.add(new BasicNameValuePair("token", URLEncoder
							.encode(LoginInfo.getInstance().getAccess_token(),
									"UTF-8")));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}

				HttpResultJson result = NetService.httpPutReturnJson(
						context,
						Config.HOME_URL
								+ String.format(Config.SET_PERSONINFO_URL, code),
						params);

				if (result.getResultCode() == HttpResult.RESULT_OK) {
					apiResult.setResultCode(ApiResult.RESULT_OK);
					JSONObject jsonObject = result.getJson();
					apiResult.setJsonEntity(jsonObject);
				} else {
					apiResult.setFailCode(result.getFailCode());
					apiResult.setFailMessage(result.getFailMessage());
				}
				return apiResult;
			}

			@Override
			protected void onPostExecute(ApiResult<Object> apiResult) {
				if (returnResultListener == null) {
					return;
				}
				if (apiResult.getResultCode() == ApiResult.RESULT_OK) {
					returnResultListener.onReturnSucceedResult(requestCode,
							apiResult);
				} else {
					returnResultListener.onReturnFailResult(requestCode,
							apiResult);
				}
			}

			@Override
			protected void onPreExecute() {
				if (returnResultListener == null) {
					return;
				}
				returnResultListener.onReturnLoading(requestCode);
				super.onPreExecute();
			}

		}.execute();
	}
}
